package com.thread.demo10;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class ThreadPool {
	public static void main(String[] args) throws InterruptedException {
		System.out.println(TimeUnit.MILLISECONDS.toNanos(0));
		
		int count = 19;
//		final ExecutorService fixedThreadPool = Executors.newFixedThreadPool(25);
		final ThreadPoolExecutor fixedThreadPool =  new ThreadPoolExecutor(25, 25,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>());
//		final ThreadPoolExecutor fixedThreadPool =  new ThreadPoolExecutor(8, 16, 60, TimeUnit.SECONDS,
//                new LinkedBlockingQueue<>(32), new ThreadPoolExecutor.CallerRunsPolicy());
		AtomicInteger atomicInteger = new AtomicInteger(1);
		for(int i=0;i<count;i++){
			Thread.sleep(1);
			int index = i;
			fixedThreadPool.execute(()->{
				int a = index;
				try {
					Thread.sleep(1*index);
				} catch (Exception e) {
					e.printStackTrace();
				}
				System.out.println("序号"+index+LocalDateTime.now()
				+":"+fixedThreadPool.getCorePoolSize()
				+":"+fixedThreadPool.getCompletedTaskCount()
				+":"+fixedThreadPool.getActiveCount());
//				System.out.println(fixedThreadPool.getActiveCount());
//				if(atomicInteger.getAndIncrement() == 1){
//					System.out.println("最后线程结束，关闭线程池。。。");
////					fixedThreadPool.shutdown();
//					System.out.println("最后线程结束，关闭线程池。。。是否关闭"+fixedThreadPool.isShutdown());
//				}
			});
		}
		
		new Thread(()->{
			try {
				Thread.sleep(1500);
			} catch (Exception e) {
				e.printStackTrace();
			}
			System.out.println("是否关闭"+fixedThreadPool.isShutdown());
			
			System.out.println("over"+LocalDateTime.now()
			+":"+fixedThreadPool.getCorePoolSize()
			+":"+fixedThreadPool.getCompletedTaskCount()
			+":"+fixedThreadPool.getActiveCount());
		}).start();
		
		
	}
}
